<%--
  Created by IntelliJ IDEA.
  User: 李昭富
  Date: 2024/5/31
  Time: 16:35
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" language="java" %>
<%@ page import = "java.sql.*" %>
<%@ include file = "jdbc.jsp" %>
<%
    //1、获得连接
    Connection conn=getConnection();
    //2、建立Statement对象
    Statement stmt=conn.createStatement();
    //3、生成sql
    String username=request.getParameter("username");
    String password=request.getParameter("password");
    String repassword=request.getParameter("repassword");
    String nickname=request.getParameter("nickname");
    int sex=Integer.parseInt(request.getParameter("sex"));
    //表单验证
    if(username.isEmpty()){
        out.print("<script>alert('用户名不能为空');history.back();</script>");
        return;
    }
    if(!password.equals(repassword)){
        out.print("<script>alert('两次密码不一致');history.back();</script>");
        return;
    }
    String sql="select * from user where username='" + username + "'";
    ResultSet rs=stmt.executeQuery(sql);
    if(rs.next()){
        out.print("<script>alert('用户名已存在');history.back();</script>");
        return;
    }
    sql="insert into user(username,password,nickname,sex) value('" + username + "','"+ password +"','" +  nickname + "'," + sex + ")";
    //4、执行Sql
    int result=stmt.executeUpdate(sql);
    //5、处理结果
    out.print("注册成功！");
%>
